
var mysql = require('mysql');
var sql_s = "Create TABLE IF NOT EXISTS  users ("    
    +"id INT(11) NOT NULL AUTO_INCREMENT COMMENT '索引键',"
    +"phone VARCHAR(11) NOT NULL COMMENT '手机',    "
    +"address VARCHAR(40) DEFAULT '' COMMENT '地址',      "
    +"PRIMARY KEY  (id,phone),"
    +"INDEX(phone));"
  
  +"Create TABLE IF NOT EXISTS  integral(    "
  +"id INT(11) NOT NULL AUTO_INCREMENT COMMENT '索引键',    "
  +"user_phone VARCHAR(11) NOT NULL  COMMENT '电话号码',   " 
  +"score INT(11) DEFAULT 0 COMMENT '积分',    "
  +"PRIMARY KEY  (id,user_phone),    "
  +"INDEX(user_phone)"
  +");"
  
  +" alter table integral add constraint fk_users_id"
  +" foreign key (user_phone) references users(phone);"
var connection= mysql.createConnection({
    multipleStatements: true,
host:'localhost',
user:'root',
password:'root',
port:3306,
database:'wx'
});

connection.connect(function(err){
    if(err){
        console.log("链接失败");
       
        throw(err)
    }else{
    console.log("链接成功");
        //查询user integral 是否存在
        //不存在则创建
        connection.query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='users' or TABLE_NAME='integral'",function(err,result){
            console.log(result);
            if(result.length==0){
                connection.query(sql_s, function(err,result){
                    if(err){
                        throw err
                    }else{
                        console.log("创建表成功")
                    }
                });
            }
            
        });
   
    }
});





module.exports = {
    mysql:{
        host:"127.0.0.1",
        user:'root',
        password:'root',
        database:'wx',
        port:'3306'
    }



}